package com.ideabove.heart;
import com.ideabove.heart.manager.AppManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.LogUtil;

import android.graphics.PorterDuff.Mode;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class SettingActivity extends MyActivity {
	Activity mActivity;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
				| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
				| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 

		mActivity = this;
		
		
		// 효과음 크기(배경음악)
		SeekBar bg_sound = (SeekBar) findViewById(R.id.bg_sound);	
		float v = ScheduleManager.getCurrentBgVolume(this);
		int percent = (int)(v * 100);
		bg_sound.setProgress(percent);
		
		bg_sound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				
				float realVolum = (float)seekBar.getProgress() / 100f;
				AppManager.setBgVolume(realVolum);
				
				ScheduleManager.setCurrentBgVolume(SettingActivity.this, realVolum);
			}
		});
		
		
		// 목소리 크기
		SeekBar voice_sound = (SeekBar) findViewById(R.id.voice_sound);	
		v = ScheduleManager.getCurrentVoiceVolume(this);
		percent = (int)(v * 100);
		voice_sound.setProgress(percent);
		
		voice_sound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				
				float realVolum = (float)seekBar.getProgress() / 100f;				
				ScheduleManager.setCurrentVoiceVolume(SettingActivity.this, realVolum);
			}
		});
		
		// 텍스트 온오프
		Button setting_btn_text_onoff = (Button)findViewById(R.id.setting_btn_text_onoff);
		boolean isTextOn = ScheduleManager.isTextOnOff(SettingActivity.this);
		if(isTextOn){ 
			// text on
			setting_btn_text_onoff.setBackgroundResource(R.drawable.btn_bg_green);
			setting_btn_text_onoff.setText("ON");
		}else{
			// text off
			setting_btn_text_onoff.setBackgroundResource(R.drawable.btn_bg_gray);
			setting_btn_text_onoff.setText("OFF");
		}		
		setting_btn_text_onoff.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if(event.getAction() == MotionEvent.ACTION_UP){
					Button view = (Button)v;					
					boolean isTextOn = ScheduleManager.isTextOnOff(SettingActivity.this);
					
					if(isTextOn){
						// text on -> off
						view.setBackgroundResource(R.drawable.btn_bg_gray);
						view.setText("OFF");
						ScheduleManager.setTextOnOff(SettingActivity.this, false);
					}else{
						// text off -> on
						view.setBackgroundResource(R.drawable.btn_bg_green);
						view.setText("ON");
						ScheduleManager.setTextOnOff(SettingActivity.this, true);
					}
				}				
				return false;
			}
		});
		
		// 스토리 온오프
		Button setting_btn_story_onoff = (Button)findViewById(R.id.setting_btn_story_onoff);
		boolean isStoryOn = ScheduleManager.getStoryOnOff(SettingActivity.this);
		if(isStoryOn){ 
			// text on
			setting_btn_story_onoff.setBackgroundResource(R.drawable.btn_bg_green);
			setting_btn_story_onoff.setText("ON");
		}else{
			// text off
			setting_btn_story_onoff.setBackgroundResource(R.drawable.btn_bg_gray);
			setting_btn_story_onoff.setText("OFF");
		}		
		setting_btn_story_onoff.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if(event.getAction() == MotionEvent.ACTION_UP){
					Button view = (Button)v;					
					boolean isStoryOn = ScheduleManager.getStoryOnOff(SettingActivity.this);
					if(isStoryOn){ 
						// text on -> OFF
						view.setBackgroundResource(R.drawable.btn_bg_gray);
						view.setText("OFF");
						ScheduleManager.setStoryOnOff(SettingActivity.this, false);
					}else{
						// text off -> ON
						view.setBackgroundResource(R.drawable.btn_bg_green);
						view.setText("ON");
						ScheduleManager.setStoryOnOff(SettingActivity.this, true);
					}
				}				
				return false;
			}
		});
		
		// 스토리 온오프
		Button setting_btn_story_init = (Button)findViewById(R.id.setting_btn_story_init);		
		setting_btn_story_init.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if(event.getAction() == MotionEvent.ACTION_UP){
					AlertDialog.Builder ab=new AlertDialog.Builder(SettingActivity.this); 
					ab.setMessage("초기화 하시겠습니까?");					
					ab.setPositiveButton("초기화", new OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							ScheduleManager sche = new ScheduleManager(SettingActivity.this);
							sche.storyInit();
							dialog.cancel();
						}
					});
					
					ab.setNegativeButton("취소", new OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
					});
					ab.show();
				}				
				return false;
			}
		});
		
		
		// 스토리확인
		ImageView btn_call_list_confirm = (ImageView)findViewById(R.id.btn_call_list_confirm);		
		btn_call_list_confirm.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				finish();				
				return false;
			}
		});
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AppManager.pause(SettingActivity.this);
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		
		super.onDestroy();
	}

}
